<template>
  <div>
		<el-upload
			class="avatar-uploader"
			action=""
			:show-file-list="false"
			:http-request="_upload"
			:on-success="handleAvatarSuccess"
			:before-upload="beforeAvatarUpload">
			<img v-if="imageUrl" :src="imageUrl" class="avatar">
			<i v-else class="el-icon-plus avatar-uploader-icon"></i>
			<!-- <el-progress :stroke-width="2" :percentage="percent"></el-progress> -->
		</el-upload>
	</div>
</template>

<script>
import { upload } from '@/api'
export default {
	data() {
		return {
			imageUrl: '',
			percent: 0,
		}
	},
	methods: {
		handleAvatarSuccess(res, file) {
			this.imageUrl = res;
		},
		beforeAvatarUpload(file) {
			return file;
		},
		_upload() {
			let {file, onProgress, onSuccess} = arguments[0];
			upload(file, '', (res) => {
				let {url, percent, speed} = res;
				this.percent = percent * 100;
				onProgress(percent)
				if(percent == 1 && speed == 0) {
					onSuccess(url)
					onProgress(percent)
				}
			})
		},
	}
};
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>